$(function () {
    $("#questionNameDetail").html("<" +
        getCookie("nameOfQuestionnaire") + ">的相关设置");
    //传回用户名
    var da = {
        "user": getCookie("userName")
    };
    //添加下拉选择问卷
    var selectContent = ''; //下拉选择内容
    jQuery.ajax({
        type: "POST",
        url: httpRequestUrl + "/queryAllQuestionnaireByCreated",
        data: JSON.stringify(da),
        dataType: 'json',
        contentType: "application/json",
        success: function (result) {
            for (var i = 0; i < result.data.length; i++) {
                selectContent += '<option value="' + result.data[i].id + '">' +
                    result.data[i].questionName + '</option>'
            }
            $("#ddlActivitynew").html(selectContent)
            $("#ddlActivitynew option[value='" + getCookie("questionId") + "']").attr(
                "selected", "selected");
            console.log(getCookie("questionId"));
            getQuestionBaseData(getCookie("questionId"));
        }
    });


});

//    切换问卷
$("#ddlActivitynew").change(function () {
    var activity = $(this).val();
    var nameQuestion = $(this)[0].selectedOptions[0].innerHTML;
    if (activity) {
        deleteCookie("questionId");
        setCookie("questionId", activity)
        deleteCookie("nameOfQuestionnaire");
        setCookie("nameOfQuestionnaire", nameQuestion)
        $("#questionNameDetail").html("<" +
            getCookie("nameOfQuestionnaire") + ">答题情况明细");
        console.log(activity);
        getQuestionBaseData(activity);
    }
})

//得到问卷基础数据
function getQuestionBaseData(id) {
    var da = {
        "questionId": id
    }
    commonAjaxPost(true,"/queryQuestionnaireById", da, function (result) {
        console.log(result.data);
        $("#questionName").val(result.data.questionName);
        var answerTotal= result.data.answerTotal;
        $("#answerTotalNum").val(answerTotal);
        var endTime = result.data.endTime.replace(/-/g, '/').replace(/T/g, " ");
        var startTime = result.data.startTime.replace(/-/g, '/').replace(/T/g, " ");
        $("#questionStartEndTime").val(startTime + " ~ " + endTime);
        createTimePicker(startTime, endTime);
    });
}

//设计问卷
function designQuestionnaire() {
    var ifDesignQuestionnaire = getCookie("ifDesignQuestionnaire");
    if (ifDesignQuestionnaire == "false") {
        layer.msg("问卷处于运行状态或问卷已发布，不可设计问卷", {icon: 2})
    } else {
        $.cookie("QuestionId", getCookie("questionId"));
        window.open('designQuestionnaire.html')
    }
}

//发送问卷
function ifSendQuestionnaire() {
    var ifSendQuestionnaire = getCookie("ifSendQuestionnaire");
    if (ifSendQuestionnaire === "false") {
        layer.msg("问卷处于未发送或暂停的状态，不可发送问卷", {icon: 2})
    } else {
        $.cookie("QuestionId", getCookie("questionId"));
        window.location.href = 'sendQuestionnaire.html';
    }
}

//回车事件
$(document).keydown(function (event) {
    if (event.keyCode === 13) {
    }
});

//返回主页
function backhome() {
    window.location.href = 'myQuestionnaires.html'
}


function setQuestionOption(){
    var ifSendQuestionnaire = getCookie("ifSendQuestionnaire");
    if (ifSendQuestionnaire === "false") {
        layer.msg("问卷处于未发送或暂停的状态，不可设计问卷", {icon: 2})
    } else {
        $.cookie("QuestionId", getCookie("questionId"));
        window.location.href = 'setQuestionAttribute.html';
    }
}

//点击“保存修改”，编辑问卷
function modifyQuest() {
    var questionNameInt = $("#questionName").val();
    var questionStarTimeInt = $("#questionStartEndTime").val().split(" ~ ")[0];
    var questionEnTimeInt = $("#questionStartEndTime").val().split(" ~ ")[1];

    var questionStarTimeIntTemp = dateChange(questionStarTimeInt);
    var questionEnTimeIntTemp = dateChange(questionEnTimeInt);
    debugger
    if (questionNameInt.trim() === '') {
        layer.msg('请完整填写问卷调查名称')
    } else if (questionNameInt.trim().length > 20) {
        layer.msg('问卷调查名称不能超过20个字,请重新修改')
    } else {
        var data = {
            "id": getCookie("questionId"),
            "questionName": questionNameInt,
            "startTime": questionStarTimeIntTemp,
            "endTime": questionEnTimeIntTemp,
            "answerTotal": $("#answerTotalNum").val()
        };
        commonAjaxPost(true, '/modifyQuestionnaireInfo', data, modifyQuestSuccess);
    }
}

//修改问卷信息成功
function modifyQuestSuccess(result) {
    if (result.code === '666') {
        layer.msg('修改成功', {icon: 1});
        setTimeout(function () {
            setCookie('ifEditQuestType', 'true');
            //window.location.href = 'projectInfo.html';
        }, 1000);
    } else if (result.code === "333") {
        layer.msg(result.message, {icon: 2});
        setTimeout(function () {
            window.location.href = 'login.html';
        }, 1000)
    } else {
        layer.msg(result.message, {icon: 2});
    }
}

//预览问卷
$('#ctl02_hrefView').click(function () {
    window.open('previewQuestionnaire.html?i=' + getCookie("questionId") + '&e0Rp=preview');
});
//回车事件
$(document).keydown(function (event) {
    if (event.keyCode == 13) {
        getQuestionnaireAboutSchool();
    }
});

// 创建时间区域选择
function createTimePicker(startTime, endTime) {
    var nowTime = getFormatDateSecond();
    $('#questionStartEndTime').daterangepicker({
        "minDate": nowTime,
        "startDate": startTime,
        "endDate": endTime,
        "timePicker": true,
        "timePicker12Hour": true,
        "linkedCalendars": false,
        // "autoUpdateInput": false,
        "locale": {
            "resetLabel": "重置",
            "format": 'YYYY/MM/DD HH:mm:ss',
            "separator": " ~ ",//
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "起始时间",
            "toLabel": "结束时间'",
            "customRangeLabel": "自定义",
            "weekLabel": "W",
            "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
            "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月",
                "十一月", "十二月"],
            "firstDay": 1
        },
    }, function (start, end, label) {
    });
}
